﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cn.Edwon.Web.Support;
using System.Reflection;
using Cn.Cellula.Model;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using Cn.Edwon.Web.Validation;
using Cn.Edwon.Web;
using Cn.Cellula.Biz;
using Cn.Cellula.Website.Ws;
using Cn.Cellula.Model.System;
using Cn.Cellula.Client;
namespace Cn.Cellula.Website.Admin.Content.Action {
    public partial class Edit : PageSupport{
        String EOKey = null;
        IUserBiz biz = BizFactory.Get<IUserBiz>();
        protected void Page_Load(object sender, EventArgs e) {
            EOKey = Request.QueryString["id"];
            if (!Page.IsPostBack) {
                InitForm();
                if (!String.IsNullOrEmpty(EOKey)) {
                    FillForm(EOKey);
                }
            }
        }

        void InitForm() {
            ClientBiz.BindCodeList(ref this.ddl_Type, CodeTypeDef.Action.Key, true, null);
        }
        void FillForm(String key) {
            TSysActionEO eo = biz.Load<TSysActionEO>(key);
            this.txt_ActionName.Text = eo.ActionName;
            this.txt_Hql.Text = eo.Hql;
            this.ddl_Type.SelectedValue = eo.ActionType;
            this.txt_Desc.Text = eo.Description;
        }

        protected void btn_save_Click(object sender, EventArgs e) {
            Boolean bk = QuickValidator.Execute(Request, this.txt_ActionName.Parent);
            if (!bk) {
                SiteInfo.Send(Page, TxtDef.ERROR, QuickValidator.Message);
                return;
            }
            if (String.IsNullOrEmpty(EOKey)) {
                TSysActionEO eo = new TSysActionEO();
                eo.ActionName = this.txt_ActionName.Text;
                eo.ActionType = this.ddl_Type.SelectedValue;
                eo.Hql = this.txt_Hql.Text;
                eo.Description = this.txt_Desc.Text;
                biz.Save<TSysActionEO>(eo);
                BizFactory.Get<ILogBiz>().LogInsert("动作",eo.ActionName, this.Actor, Request);
            } else {
                TSysActionEO eo = biz.Load<TSysActionEO>(EOKey);
                eo.ActionName = this.txt_ActionName.Text;
                eo.ActionType = this.ddl_Type.SelectedValue;
                eo.Hql = this.txt_Hql.Text;
                eo.Description = this.txt_Desc.Text;
                biz.Update<TSysActionEO>(eo);
                BizFactory.Get<ILogBiz>().LogUpdate("动作", eo.ActionName, this.Actor, Request);
            }
            Response.Redirect("List.aspx");
        }

        protected void btn_Test_Click(object sender, EventArgs e) {
            try {
                _Action act = new _Action();
                String hql = act.BuildHql(this.txt_Hql.Text, this.txt_Parms.Text);
                this.txt_Hql.Text = hql;
                this.txt_Result.Text = act.BuildJson(hql,this.ddl_Type.SelectedValue);
            } catch (Exception ex) {
                this.txt_Result.Text = ex.Message + "\n" + ex.StackTrace;
            }
        }
    }
}
